package com.wifeacountbook;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BaseListActivity extends Activity implements OnItemClickListener,OnClickListener{

	static final int PAY_TIME_DIALOG_ID = 2;
	protected WeakReference<BaseListActivity> localref = new WeakReference<BaseListActivity>( this);
	public BaseListActivity() {
		super();
	}

	protected void setMonLabel(Date d) {
		Button btn = (Button) findViewById(R.id.order);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String labelText = sdf.format(d)+" "+getText(R.string.str_order);
		btn.setText(labelText);
	}
	
	protected String getAddAction(){return "";}
	protected String getViewAction(){return "";}
	protected Uri getUri(){return null;}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
		TextView tv = (TextView) v.findViewById(R.id.record_id);
		String txt = tv.getText().toString();
		if(!TextUtils.isEmpty(txt)&&TextUtils.isDigitsOnly(txt)){
			long id2 = Long.parseLong(txt);
			Uri viewUri = ContentUris.withAppendedId(getUri(), id2);
			Intent intent = new Intent(getViewAction());
			intent.setData(viewUri);
			intent.setClass((BaseListActivity)localref.get(), AcountRecorder.class);
			startActivity(intent);
		}
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.button1){
			Intent intent = new Intent(getAddAction());
			intent.setClass((BaseListActivity)localref.get(), AcountRecorder.class);
			startActivity(intent);
		}else if(v.getId() == R.id.order){
			showDialog(PAY_TIME_DIALOG_ID);
		}
	}

}